layui.use(['layer', 'form', 'table', 'ztree', 'laydate','laypage', 'admin', 'ax', 'func', 'tree', 'util'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ZTree = layui.ztree;
    var $ax = layui.ax;
    var laydate = layui.laydate;
    var laypage = layui.laypage;
    var admin = layui.admin;
    var func = layui.func;
    var tree = layui.tree;
    var util = layui.util;


    /**
     * 系统管理--用户管理
     */
    var MgrUser = {
        tableId: "userTable",    //表格id
        condition: {
            name: "",
            deptId: "",
            timeLimit: ""
        }
    };

    /**
     * 遍历获取常用的日程
     */
    var count1;
    var pageSize = 1;

    var queryData = {};
    loadSchedule(queryData);
    loadTodaySchedule();

    /**
     * 遍历获取常用的日程
     */
    function loadSchedule(dataa){
        if (dataa==null){
            dataa ={}
        }
        /**
         * 获取四条数据
         */
        $.ajax({
            url:Feng.ctxPath + "/schedule/schedule_list",
            data: dataa,
            dataType:"json",
            success:function(pageMessage){
                if (pageMessage.count !=0){
                    $('#scheduleIndex').empty();
                    layui.each(pageMessage.data, function (index, item) {
                        if (index < 4) {
                            var lock = index + 'lock';
                            var editId = index + 'edit';
                            var str = '';
                            str += '<div class="layui-col-xs12 layui-col-sm6 layui-col-md3">';
                            str += '    <div class="myui-card">';
                            str += '        <div class="layui-card-header">' + item.scheduleName + '<span class="pull-right">';
                            if (item.publicStatus == "ENABLE") {
                                if (item.postStatus == "DISABLE") {
                                    str += '            <i class="layui-icon layui-icon-log" id=' + lock + '></i>';
                                }
                                str += '            <i class="layui-icon layui-icon-upload" id=' + lock + '></i></span></div>';
                            } else {
                                if (item.postStatus == "DISABLE") {
                                    str += '            <i class="layui-icon layui-icon-log" id=' + lock + '></i>';
                                }
                                str += '            <i class="layui-icon layui-icon-password" id=' + lock + '></i></span></div>';
                            }
                            str += '        <div class="layui-card-body">';
                            str += '            <p class="myFont" id="scheduleName">';
                            str += item.description != "" ? item.description : "它还没有描述哦～～";
                            str += '            </p>';
                            str += '        </div>';
                            str += '        <ul class="icon">';
                            str += '            <li id ="viewSchedule' + index + '"><a><i class="layui-icon layui-icon-date"></i></a></li>';
                            str += '            <li id ="editSchedule' + index + '"><a><i class="layui-icon layui-icon-edit"></i></a></li>';
                            if (item.postStatus == "DISABLE") {
                                str += '            <li id ="releSchedule' + index + '"><a><i class="layui-icon layui-icon-share"></i></a></li>';
                            }
                            str += '            <li id ="deleteSchedule' + index + '"><a><i class="layui-icon layui-icon-delete"></i></a></li>';
                            str += '        </ul>';
                            str += '    </div>';
                            str += '</div>';

                            $('#scheduleIndex').append(str);

                            /**
                             * 进入日历框架
                             */
                            $('#viewSchedule' + index).click(function () {
                                // alert("点击了日历按钮");
                                location.href = Feng.ctxPath + "/schedule/details?scheduleId=" + item.scheduleId;
                            });

                            /**
                             * 保存后编辑框架
                             */
                            $('#editSchedule' + index).click(function () {
                                // frameId = item.frameId;
                                // func.open({
                                //     title: '编辑'+item.frameName+'框架',
                                //     content: Feng.ctxPath + '/frame/edit?frameId=' + item.frameId,
                                //     tableId: MgrUser.tableId
                                // });

                                layer.open({
                                    type: 2,
                                    title: '编辑' + item.scheduleName + '框架',
                                    area: ['90%', '95%'],
                                    fixed: false, //不固定
                                    maxmin: true,
                                    content: Feng.ctxPath + '/schedule/edit?scheduleId=' + item.scheduleId,
                                    success: function () {
                                    },
                                    end : function() {
                                        location.reload();
                                    }
                                });

                            });

                            /**
                             * 发布框架
                             */
                            $('#releSchedule' + index).click(function () {
                                var operation = function () {
                                    var ajax = new $ax(Feng.ctxPath + "/frame/releaseFrame", function () {
                                        table.reload(MgrUser.tableId);
                                        Feng.success("发布成功!");
                                    }, function (data) {
                                        Feng.error("发布失败!" + data.responseJSON.message + "!");
                                    });
                                    ajax.set("frameId", item.frameId);
                                    ajax.start();
                                    //刷新当前页面
                                    location.reload();
                                };
                                Feng.confirm("是否发布" + item.frameName + "框架?发布后内容不可修改", operation);
                            });

                            /**
                             * 删除框架
                             */
                            $('#deleteSchedule' + index).click(function () {
                                var operation = function () {
                                    var ajax = new $ax(Feng.ctxPath + "/schedule/schedule_delete", function () {
                                        table.reload(MgrUser.tableId);
                                        Feng.success("删除成功!");
                                        //刷新当前页面
                                        // location.reload();
                                        //回调当前页面
                                        loadSchedule(dataa);
                                    }, function (data) {
                                        Feng.error("删除失败!" + data.responseJSON.message + "!");
                                    });
                                    ajax.set("scheduleId", item.scheduleId);
                                    ajax.start();

                                };
                                Feng.confirm("是否删除" + item.scheduleName + "日程?", operation);
                            });
                        }
                    });
                }
            }
        });


        // var pageMessage = new $ax(Feng.ctxPath + "/frame/frame_list",dataa,"json").start();

    }

    function loadTodaySchedule() {
        $.ajax({
            url:Feng.ctxPath + "/schedule/getTodaySchedule",
            success:function (result) {

                if (result.data.length !=0){
                    $('#todaySchedule').empty();
                    layui.each(result.data,function (index,item) {
                        var html = '';
                        html += '<li class="layui-timeline-item">';
                        html += '   <i class="layui-icon layui-timeline-axis">&#xe63f;</i>';
                        html += '   <div class="layui-timeline-content layui-text">';
                        html += '       <h3 class="layui-timeline-title">日程表：'+ item.scheduleName +'&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;<span class="layui-badge-rim">'+ item.scheduleitemDay +'</span></h3>';
                        html += '       <ul>';
                        layui.each(item.scheduleitems,function (insideIndex,insideItem) {
                            html += '       <li >';
                            html += '            <p class="text-danger" style="font-size: 16px;">'+insideItem.startTime + '-' +insideItem.endTime+'&emsp;'+ insideItem.scheduleitemName +'</p>';
                            html += '            <p>地点：'+insideItem.location+'</p>';
                            html += '            <p>内容：'+insideItem.content+'</p>';
                            html += '       </li>';
                        });
                        html += '       </ul>';
                        html += '   </div>';
                        html += '</li>';
                        $('#todaySchedule').append(html);
                    });
                }
            }
        })

    }

});

